﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TranTekBE.Marketing
{
    public partial class Add_Type_Advertising : PageRequireLogin
    {
        dbMarketingDataContext db = new dbMarketingDataContext();

        protected void Page_Load(object sender, EventArgs e)
        {
            ButtonXoa.Visible = false;
            ButtonSua.Visible = false;
            LabelError.Text = "";
            if (!IsPostBack)
            {
                LoadGridView();
            }
        }
        protected void Page_PreRender(object sender, EventArgs e)
        {
            if (GridView1.SelectedIndex >= 0)
            {
                ButtonXoa.Visible = true;
                ButtonSua.Visible = true;
            }
            else
            {
                ButtonXoa.Visible = false;
                ButtonSua.Visible = false;
            }
        }
        public void LoadGridView()
        {
            GridView1.DataSource = db.vLoaiQuangCaos;
            GridView1.DataBind();
            if (GridView1.SelectedIndex >= 0)
            {
                ButtonXoa.Visible = false;
                ButtonSua.Visible = false;
            }
        }

        protected void ButtonThem_Click(object sender, EventArgs e)
        {
            db.LoaiQuangCaos.InsertOnSubmit(new LoaiQuangCao()
            {
                Name = TextBoxLoaiQuangCao.Text,
                CreatedBy = UserInfo.UserID,
                CreatedDate = DateTime.Now,
                UpdatedDate = DateTime.Now,
                UpdatedBy = UserInfo.UserID
            });
            db.SubmitChanges();
            GridView1.SelectedIndex = -1;
            TextBoxLoaiQuangCao.Text = "";
            LoadGridView();
            LabelError.Text = "Thêm thành công";
            LabelError.ForeColor = Color.Green;
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int id = int.Parse(GridView1.SelectedRow.Cells[1].Text);
            var model = db.LoaiQuangCaos.FirstOrDefault(c => c.LoaiQuangCaoId == id);
            if (model != null)
            {
                TextBoxLoaiQuangCao.Text = model.Name;

            }
            else
            {
                LabelError.Text = "Lối. Không tìm thấy id: " + id;
                LabelError.ForeColor = Color.Red;
            }

        }

        protected void ButtonXoa_Click(object sender, EventArgs e)
        {
            int id = int.Parse(GridView1.SelectedRow.Cells[1].Text);
            var model = db.LoaiQuangCaos.FirstOrDefault(c => c.LoaiQuangCaoId == id);
            if (model != null)
            {
                db.LoaiQuangCaos.DeleteOnSubmit(model);
                db.SubmitChanges();
                LoadGridView();
                LabelError.Text = "Xóa thành công";
                LabelError.ForeColor = Color.Green;
            }
            else
            {
                LabelError.Text = "Lối. Không tìm thấy id: " + id;
                LabelError.ForeColor = Color.Red;
            }
            GridView1.SelectedIndex = -1;
        }

        protected void ButtonSua_Click(object sender, EventArgs e)
        {
            int id = int.Parse(GridView1.SelectedRow.Cells[1].Text);
            var model = db.LoaiQuangCaos.FirstOrDefault(c => c.LoaiQuangCaoId == id);
            if (model != null)
            {
                model.Name = TextBoxLoaiQuangCao.Text;
                db.SubmitChanges();
                LoadGridView();
                LabelError.Text = "Sửa thành công";
                LabelError.ForeColor = Color.Green;
            }
            else
            {
                LabelError.Text = "Lối. Không tìm thấy id: " + id;
                LabelError.ForeColor = Color.Red;
            }
            GridView1.SelectedIndex = -1;
        }
    }
}